package com.itheima04;

import java.util.concurrent.locks.ReentrantLock;

public class SellTicket implements Runnable {
    private int ticket = 100;
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
//        synchronized (this){
            try {
                lock.lock();
                if (ticket == 0) {
                    break;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ticket--;
                System.out.println(Thread.currentThread().getName() + "卖了一张，还剩" + ticket + "张");

            }
            finally {
                lock.unlock();
            }
        }
    }
//    }
}